package designPattern.singleton;

/**
 * 优化 synchronized 重量级锁
 * 但产生了并发问题
 */
public class singleton04 {

    private static singleton04 INSTANCE;

    private singleton04() {
    }

    public static  singleton04 getInstance(){
        if (INSTANCE == null) {
            //减少同步代码块的方式提高效率 但是又产生并发问题
            synchronized (singleton04.class) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                INSTANCE = new singleton04();
            }
        }
        return INSTANCE;
    }

    public void k(){
        System.out.println("OK");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread( ()-> System.out.println(singleton04.getInstance().hashCode())).start();
        }
    }
}
